<?php

global $_W, $_GPC;
$operation = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($operation == 'display') {
    ca('order.view');
    $pindex = max(1, intval($_GPC['page']));
    $psize = 20;
    $exchanges = pdo_fetchall('SELECT id, title FROM ' . tablename('activity_exchange') . ' WHERE uniacid = :uniacid ORDER BY id DESC', array(':uniacid' => $_W['uniacid']));
    $starttime = empty($_GPC['time']['start']) ? strtotime('-1 month') : strtotime($_GPC['time']['start']);
    $endtime = empty($_GPC['time']['end']) ? TIMESTAMP : strtotime($_GPC['time']['end']);
    $where = " WHERE a.uniacid=:uniacid ";
    $params = array(':uniacid' => $_W['uniacid']);
    if ($_GPC['searchtime'] == 1) {
        $where.=" AND a.createtime>=:starttime AND a.createtime<:endtime";
        $params[':starttime'] = $starttime;
        $params[':endtime'] = $endtime;
    }
    $uid = intval($_GPC['uid']);
    if (!empty($uid)) {
        $where .= ' AND a.uid=:uid';
        $params[':uid'] = $uid;
    }
    if (!empty($_GPC['status']) || $_GPC['status'] === "0") {
        $where .= ' AND a.status=:status';
        $params[':status'] = $_GPC['status'];
    }
    $exid = intval($_GPC['exid']);
    if (!empty($exid)) {
        $where .= " AND b.id = {$exid}";
    }
    if (!empty($_GPC['ordersn'])) {
        $where .= ' AND a.tid=:tid';
        $params[':tid'] = trim($_GPC['ordersn']);
    }
    if (!empty($_GPC['expresssn'])) {
        $where .= ' AND a.expresssn=:expresssn';
        $params[':expresssn'] = trim($_GPC['expresssn']);
    }
    if (!empty($_GPC['receiver'])) {
        $where .= ' AND a.name=:name';
        $params[':name'] = trim($_GPC['receiver']);
    }
    if (!empty($_GPC['member'])) {
        $_GPC['member'] = trim($_GPC['member']);
        $where .= " AND (m.realname LIKE '%{$_GPC['member']}%' or m.nickname LIKE '%{$_GPC['member']}%' or m.mobile LIKE '%{$_GPC['member']}%')";
    }
    $list = pdo_fetchall("SELECT a.*, b.title,b.extra,b.thumb,m.avatar,m.nickname FROM " . tablename('activity_exchange_trades_shipping') . ' AS a LEFT JOIN ' . tablename('activity_exchange') . ' AS b ON a.exid = b.id LEFT JOIN  ' . tablename('ewei_shop_member') . " AS  m ON a.uid=m.uid  $where ORDER BY tid DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize, $params);
    $total = pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('activity_exchange_trades_shipping') . ' AS a LEFT JOIN ' . tablename('activity_exchange') . ' AS b ON a.exid = b.id  LEFT JOIN ' . tablename('ewei_shop_member') . " AS  m ON a.uid=m.uid  $where ", $params);
    if (!empty($list)) {
        foreach ($list as &$row) {
            $row['avatar'] = $row['avatar'];
            $row['nickname'] = $row['nickname'];
            $row['extra'] = iunserializer($row['extra']);
            $row['thumb'] = tomedia($row['thumb']);
            $row['createtime'] = date('Y-m-d H:i:s', $row['createtime']);
        }
    }
    $pager = pagination($total, $pindex, $psize);
    //导出模板
    if($_GPC['submit_modal'] == "导出模板"){
        ca('order.op.export');
        $columns =array(array('title' => '订单编号', 'field' => 'tid', 'width' => 24),array('title' => '快递公司', 'field' => 'express', 'width' => 24),array('title' => '快递单号', 'field' => 'expresssn', 'width' => 24));
        m('excel')->export(" ", array("title" => "真实物品兑换待发货订单模板-" . date('Y-m-d-H-i', time()), "columns" => $columns));
    }
    //批量发货
    if ($_GPC['submit_expro'] == "批量发货") {
        ca('order.op.send');
        $info =m('excel')->import("import_expro");
        foreach ($info as $key => $value) {
            $value[0]=trim($value[0]);$value[1]=trim($value[1]);$value[2]=trim($value[2]);
            $item = pdo_fetch("SELECT a.*,b.title FROM " . tablename('activity_exchange_trades_shipping') . "AS a LEFT JOIN " . tablename('activity_exchange') . "AS b ON a.exid=b.id WHERE a.tid = :tid and a.uniacid=:uniacid", array(':tid' => $value[0], ':uniacid' => $_W['uniacid']));
            if ($item['status'] == 0 && !empty($value[0]) && !empty($value[1]) && !empty($value[2]) && !empty($item)) {
                pdo_update('activity_exchange_trades_shipping', array('status' => 1, "express" => $value[1], "expresssn" => $value[2], "sendtime" => time()), array("tid" => $value[0], 'uniacid' => $_W['uniacid']));
                $userinfo = pdo_fetch('select * from ' . tablename('ewei_shop_member') . 'where uid=:uid and uniacid=:uniacid', array(':uid' => $item['uid'], ':uniacid' => $_W['uniacid']));
                $msg = array(
                    'first' => array('value' => "真实物品发货通知！", "color" => "#4a5077"),
                    'orderProductName' => array('title' => '商品名称', 'value' => $item['title'], "color" => "#4a5077"),
                    'orderProductfuwu' => array('title' => '物流服务', 'value' => $value[1], "color" => "#4a5077"),
                    'orderProductdanhao' => array('title' => '快递单号', 'value' => $value[2], "color" => "#4a5077"),
                    'orderProductxinxi' => array('title' => '收货信息', 'value' => '地址：' . $item['province'] . $item['city'] . $item['district'] . $item['address'] . ' 收货人：' . $item['name'] . ' 电话:' . $item['mobile'], "color" => "#4a5077"),
                    'remark' => array('value' => "\r\n我们正在加速送到您的手上，请您耐心等待。", "color" => "#4a5077")
                );
                m('message')->sendCustomNotice($userinfo['openid'], $msg);
            }
        }
        message('批量发货成功！', referer(), 'success');
    }
    //导出记录
    if ($_GPC['export'] == 1) {
        ca('order.op.export');
        $list = pdo_fetchall("SELECT a.*, b.title,b.extra,b.thumb,m.avatar,m.nickname FROM " . tablename('activity_exchange_trades_shipping') . ' AS a LEFT JOIN ' . tablename('activity_exchange') . ' AS b ON a.exid = b.id  LEFT JOIN' . tablename('ewei_shop_member') . " AS  m ON a.uid=m.uid  $where and a.name != '' and a.mobile != '' ORDER BY tid DESC ", $params);
        $columns = array(
            array('title' => '订单ID', 'field' => 'tid', 'width' => 12),
            array('title' => '昵称', 'field' => 'nickname', 'width' => 12),
            array('title' => '兑换物品', 'field' => 'title', 'width' => 12),
            array('title' => '收件人', 'field' => 'name', 'width' => 12),
            array('title' => '收件人电话', 'field' => 'mobile', 'width' => 12),
            array('title' => '邮编', 'field' => 'zipcode', 'width' => 12),
            array('title' => '收件地址', 'field' => 'addresses', 'width' => 12),
            array('title' => '兑换时间', 'field' => 'createtime', 'width' => 12),
            array('title' => "发货时间", 'field' => 'sendtime', 'width' => 12),
            array('title' => '完成时间', 'field' => 'finishtime', 'width' => 12),
            array('title' => '订单状态', 'field' => 'status', 'width' => 12)
        );
        foreach ($list as $key => $value) {
            $list[$key]["addresses"] = $value['province'] . ' ' . $value['city'] . '' . $value['district'] . '' . $value['address'];
            $extra=iunserializer($value['extra']);
            $list[$key]['title'] =$extra['title'];
            $list[$key]['createtime'] = !empty($value['createtime']) ? date('Y-m-d H:i', $value['createtime']) : "";
            $list[$key]['sendtime'] = !empty($value['sendtime']) ? date('Y-m-d H:i', $value['sendtime']) : "";
            $list[$key]['finishtime'] = !empty($value['finishtime']) ? date('Y-m-d H:i', $value['finishtime']) : "";
            if ($value['status'] == "0") {
                $list[$key]['status'] = "待发货";
            }
            if ($value['status'] == "1") {
                $list[$key]['status'] = "已发货";
            }
            if ($value['status'] == "2") {
                $list[$key]['status'] = "已收货";
            }
        }
        m('excel')->export($list, array("title" => "真实物品兑换订单导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
    }
    load()->func('tpl');
    include $this->template('web/allorder/realgoods_list');
    exit;
}
if ($operation == 'receiver') {
    $id = intval($_GPC['id']);
    $shipping = pdo_fetch('SELECT * FROM ' . tablename('activity_exchange_trades_shipping') . ' WHERE uniacid = :uniacid AND tid = :tid', array(':uniacid' => $_W['uniacid'], ':tid' => $id));
    $shipping['sendtime'] =!empty($shipping['sendtime'])?date('Y-m-d H:i:s', $shipping['sendtime']):"";
    $shipping['createtime'] = !empty($shipping['createtime'])?date('Y-m-d H:i:s', $shipping['createtime']):"";
    $shipping['finishtime'] =!empty($shipping['finishtime'])?date('Y-m-d H:i:s', $shipping['finishtime']):"";
    if (checksubmit('submit')) {
        $data = array(
            'name' => $_GPC['realname'],
            'mobile' => $_GPC['mobile'],
            'province' => $_GPC['reside']['province'],
            'city' => $_GPC['reside']['city'],
            'district' => $_GPC['reside']['district'],
            'address' => $_GPC['address'],
            'zipcode' => $_GPC['zipcode'],
            'status' => intval($_GPC['status']),
            'express' => $_GPC['express'],
            'expresssn' => $_GPC['expresssn'],
            'createtime' => strtotime($_GPC['createtime']),
            'sendtime' => strtotime($_GPC['sendtime']),
            'finishtime' => strtotime($_GPC['finishtime'])
        );
        pdo_update('activity_exchange_trades_shipping', $data, array('tid' => $id));
        message('更新发货人信息成功', referer(), 'success');
    }
    load()->func('tpl');
    include $this->template('web/allorder/realgoods_detail');
}